/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fileuploaddownload;

import Message.AjaxUtils;
import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import javax.servlet.ServletContext;

import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;


import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

/**
 *
 * @author tomasz
 */
@Controller

public class FileUploadController {

    
    private dao.File file;
    
    @Autowired
    public void setFile(dao.File file){
        this.file = file;
    }
    
    @RequestMapping(value="/fileupload.htm",method = RequestMethod.GET)
    public String fileUploadForm(@RequestHeader(value = "X-Requested-With", required = false) String requestedWith, Model model) {

        model.addAttribute("ajaxRequest", AjaxUtils.isAjaxRequest(requestedWith));
        return "fileupload/fileupload";

    }

    @RequestMapping(value="/successupload.htm",method = RequestMethod.GET)
    public String fileUploadSuccess( Model model) {

        
        return "fileupload/successupload";

    }
    
    @RequestMapping(method = RequestMethod.POST)
    public String processUpload(@RequestParam MultipartFile file, HttpServletRequest request, Model model_map) throws IOException {


        MultipartHttpServletRequest multiPartRequest = (MultipartHttpServletRequest) request;
        MultipartFile multipartFile = multiPartRequest.getFile("file");
//        String sep = System.getProperty("file.separator");


        model.File file_ob = new model.File();
        int id_account = Integer.parseInt( request.getParameter("id_acc"));
        file_ob.setIdAccount(id_account);
        
        Calendar ca = Calendar.getInstance();
        Date date_to_day = ca.getTime();
        file_ob.setDateAdd(date_to_day);
        
        
        /**
         * Ustaw realna sciezke
         */
        ServletContext context = request.getSession().getServletContext();

        String realPath = context.getRealPath("/");
        String destinationDir = realPath + "\\temp\\";
        Random rand = new Random();
        int fileName = rand.nextInt(10000);

        String orgFileName = file.getOriginalFilename();
        file_ob.setName(orgFileName);
        
        String suffix = orgFileName.substring(orgFileName.lastIndexOf('.') + 1);
        String newFileName = fileName + "." + suffix;
        file_ob.setNameHash(newFileName);
        
        File destination = new File(destinationDir + newFileName);
        file.transferTo(destination);

        this.file.addFile(file_ob);
        
        
        List filess = this.file.allFileAccount(id_account);
        
        model_map.addAttribute("ajaxRequest", AjaxUtils.isAjaxUploadRequest(request));
        model_map.addAttribute("files",filess);
        
        return "fileupload/successupload";
    }
    
}
